<?php
/**
 * 《用料申请》实现类
 *
 * @author 孙晓晔
 * @version $Id: Purchase.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/Material.class.php');

class Used extends Material {
	public $tbl = Constant::tbl_wl_used;

	public $status = array(
		'-' => '请选择',
		'1' => '用料申请',
		'2' => '等待领料',
	// '3' => '正在领料',

		'5' => '暂停领料',
		'6' => '取消领料',

		'8' => '等待出库',
		'9' => '已经出库',

		'10' => '审核未通过',

	// '99' => '已经被锁',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'emp' => '申请人',
		'status' => '状态',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '申请人',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-3 month'));

		$controller->display($request, 'material/out/used/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'date') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['date'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$row['num_'] = $this->_num($row['id']);

			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'material/out/used');
		return true;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '%" . $name_s . "%'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'material/out/used/list');
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE used = '%s'";
		$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	function add($controller, $request) {
		$this->pretreat($controller);

		$cart = $_COOKIE['cart_used'];
		// echo $cart;
		$a = json_decode($cart);
		// print_r($a);

		$data = array();
		while (list($k, $v) = each($a)) {
			$row = $this->id(Constant::tbl_material_amount, $v[0]);
			$row['m'] = $this->id(Constant::tbl_material, $row['material']);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'material/out/used/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$purchase = $request['purchase'];
		$mold = $this->_input($request['mold']);
		$name = $request['name'];

		$content = $request['content'];

		$emp = $_SESSION['id'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mold, name, content, emp, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $mold, $name, $content, $emp);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		if (empty($code)) {
			$code = $this->_code();
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);

		$cart = $_COOKIE['cart_used'];
		// echo $cart;
		$a = json_decode($cart);
		// print_r($a);

		while (list($k, $v) = each($a)) {
			$row = $this->id(Constant::tbl_material_amount, $v[0]);
			if (isset($v[2])) {
				$amount = $v[2];
			} else {
				$amount = 1;
			}

			$fmt = "INSERT INTO %s(used, mold, place, material, name, standard, unit, price, amount, emp, createtime, modifytime) ";
			$fmt .= " VALUES('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id, $mold, $row['place'], $row['material'], $row['name'], $row['standard'], $row['unit'], $row['price'], $amount, $emp);
			debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _code() {
		$ret = '';
		$dd = sprintf("%s", date('ymd00'));

		do {
			$dd++;
			$dd = sprintf("%08d", $dd);
			$ret = "B" . $dd;
		} while ($this->_code_exist($ret));

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['mold_t'] = Name::mold($row['mold']);
		$row['emp_t'] = Name::emp($row['emp']);
		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mold = $this->_input($request['mold']);
		$name = $request['name'];
		$date = $request['date'];

		$content = $request['content'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET mold = '%s', name = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mold, $name, $date, $content, $id);
		// debug($sql);
		$db->query($sql);

		$cart = $_COOKIE['cart2'];
		// echo $cart;
		$a = json_decode($cart);
		debug(print_r($a, true));

		if (is_array($a)) {
			$fmt = "UPDATE %s SET amount = '%s', modifytime = now() WHERE id = '%s'";
			while (list($k, $v) = each($a)) {
				$row = $this->id(Constant::tbl_wl_record_out, $v[0]);
				if (isset($v[1])) {
					$row['price'] = $v[1];
				}
				if (isset($v[2])) {
					$row['amount'] = $v[2];
				}

				if (isset($v[3]) && $v[3] == '2') {
					$fmt = "DELETE FROM %s WHERE id = '%s'";
					$sql = sprintf($fmt, Constant::tbl_wl_record_out, $v[0]);
				} else {
					$sql = sprintf($fmt, Constant::tbl_wl_record_out, $row['amount'], $v[0]);
				}

				$db->query($sql);
				// debug($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$this->_record_out_status($id, $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		// $controller->assign_by_ref('title', $this->_title($row['status']));

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['mold_t'] = Name::mold($row['mold']);
		
		$row = $this->_view_t($row);

		return $row;
	}	

	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['mold_t'] = Name::mold($row['mold']);

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('title', $this->_title($row['status']));

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = sprintf("%s\n%s 审核", addslashes($request['content']), Name::emp($_SESSION['id']));
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	

	function _title($status) {
		if ($status == 1) {
			return '领料申请单';
		}

		if ($status == 2) {
			return '物料领料单';
		}

		if ($status == 8) {
			return '物料出库单';
		}

		return '用料申请单';
	}

	function record_out($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE used = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id);
		//debug($sql);
		$result = $db->query($sql);

		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['amount'] = floatval($row['amount']);

			$ret[] = $row;
		}

		return $ret;
	}

	function start($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$row = $this->id($this->tbl, $id);

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/start');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function startPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '8', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$db->query($sql);

		$this->_record_out_status($id, '8');

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function reason($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$row = $this->id($this->tbl, $id);

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $_COOKIE['id_'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$status = $request['status'];

		$this->_record_out_status($id, $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return true;
		}
			
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			/*
			 if ($this->_delete($id)) {
				continue;
				}
				*/
			$fmt = "DELETE FROM %s WHERE used = '%s'";
			$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id);
			// debug($sql);
			$db->query($sql);

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _delete($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		// debug($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] < 3 && $row['emp'] == $_SESSION['id']) {
				return false;
			}

			return true;
		}

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'material/out/used/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _record_out_status($used, $status) {
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE used = '%s'";
		$sql = sprintf($fmt, Constant::tbl_wl_record_out, $status, $used);
		$db->query($sql);
	}
	
	function out($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['mold_t'] = Name::mold($row['mold']);

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('title', $this->_title($row['status']));

		$controller->assign_by_ref('record_out', $this->record_out($id));

		$controller->display($request, 'material/out/used/out');
		return true;
	}

	/**
	 * 领料出库
	 */
	function outPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$date = $request['date'];
		$content = addslashes($request['content']);

		$row = $this->id($this->tbl, $id);

		$applicant = $row['emp'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '9', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$db->query($sql);

		$fmt = "SELECT * FROM %s WHERE used = '%s' AND status in (2, 8)";
		$sql = sprintf($fmt, Constant::tbl_wl_record_out, $id);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$name = addslashes($row['name']);
			$standard = addslashes($row['standard']);
			
			$fmt = "INSERT INTO %s(mold, material, apply, record, name, standard, unit, price, amount, date, applicant, emp, createtime) ";
			$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s',now())";

			$sql = sprintf($fmt, Constant::tbl_wl_out, $row['mold'], $row['material'], $row['used'], $row['id'], $name, $standard, $row['unit'], $row['price'], $row['amount'], $date, $applicant, $_SESSION['id']);
			// debug($sql);
			$db->query($sql);

			$this->out_($row);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _my() {
		if ($this->_audit('7')) {
			return '';
		}

		$emp = $_SESSION['id'];

		return " emp = '" . $emp . "'";
	}

	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '用料申请情况';
		
		$this->result_s = 20;
		$this->groupby_s = 'emp';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}
}
?>
